package cn.lqz.bucttrain.controller;

import cn.lqz.bucttrain.pojo.dto.TokenUserInfoDTO;
import cn.lqz.bucttrain.pojo.dto.UserLoginDTO;
import cn.lqz.bucttrain.pojo.dto.UserPasswordChangeDTO;
import cn.lqz.bucttrain.pojo.vo.PageResultVO;
import cn.lqz.bucttrain.pojo.vo.ResponseVO;
import cn.lqz.bucttrain.service.AccountService;
import cn.lqz.bucttrain.utils.ResponseUtils;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
@Slf4j
@Validated
public class AccountController {
    @Resource
    private AccountService accountService;

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public ResponseVO<TokenUserInfoDTO> login(@Valid UserLoginDTO userLoginDTO){
        TokenUserInfoDTO tokenUserInfoDTO = accountService.login(userLoginDTO);
        return ResponseUtils.success(tokenUserInfoDTO);
    }

    /**
     * 登出
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    public ResponseVO logout(@NotNull Long userId){
        accountService.logout(userId);
        return ResponseUtils.success();
    }

    /**
     * 修改密码
     * @param userPasswordChangeDTO
     * @return
     */
    @PostMapping("/changePassword")
    public ResponseVO changePassword(@Valid UserPasswordChangeDTO userPasswordChangeDTO){
        accountService.changePassword(userPasswordChangeDTO);
        return ResponseUtils.success();
    }

    /**
     * 获取短信验证码
     * @param phoneNumber
     * @return
     */
    @PostMapping("/getCheckCode")
    public ResponseVO getCheckCode(@NotEmpty String phoneNumber){
        accountService.getCheckCode(phoneNumber);
        return ResponseUtils.success();
    }

    /**
     * 验证码校验
     * @param phoneNumber
     * @param checkCode
     * @return
     */
    @PostMapping("/submitCheckCode")
    public ResponseVO submitCheckCode(@NotEmpty String phoneNumber,@NotEmpty String checkCode){
        accountService.submitCheckCode(phoneNumber,checkCode);
        return ResponseUtils.success();
    }

    /**
     * 忘记密码并修改密码
     * @param phoneNumber
     * @param newPassword
     * @return
     */
    @PostMapping("/forgetAndChangePassword")
    public ResponseVO forgetAndChangePassword(@NotEmpty String phoneNumber,@NotEmpty String newPassword){
        accountService.forgetAndChangePassword(phoneNumber,newPassword);
        return ResponseUtils.success();
    }
}
